using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using WIT.DAQ.Core.Helper;
using WIT.DAQ.Core.Models;

namespace WIT.DAQ.Core.Forms;

/// <summary>
///
/// </summary>
internal class DelayLogSettingForm : BaseForm
{
	/// <summary>
	///
	/// </summary>
	/// <param name="devices">需要监控延迟记录日志的设备</param>
	internal delegate void SaveHandler(List<Device> devices);

	/// <summary>
	/// Required designer variable.
	/// </summary>
	private IContainer components = null;

	private TreeView tvDevices;

	private Button btnConfirm;

	private Button btnCancel;

	private CheckBox cbCheckAll;

	private NumericUpDown nudLimit;

	private Label label2;

	private Label label1;

	/// <summary>
	///
	/// </summary>
	internal event SaveHandler Save;

	/// <summary>
	///
	/// </summary>
	internal DelayLogSettingForm()
	{
		InitializeComponent();
	}

	private void LaunchForm_Load(object sender, EventArgs e)
	{
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(Common.SETTING_FILE_FULL_PATH);
		XmlElement documentElement = xmlDocument.DocumentElement;
		XmlElement xmlElement = (XmlElement)documentElement.SelectSingleNode("Other");
		XmlElement xmlElement2 = (XmlElement)xmlElement.SelectSingleNode("DelayLogLimit");
		nudLimit.Value = Convert.ToInt32(xmlElement2.InnerText);
		InitDevices();
		string text = INIHelper.ReadValue("DelayLogDevices", "Ids", Common.MY_INI_PATH);
		if (string.IsNullOrEmpty(text) || text.ToUpper() == "NONE")
		{
			cbCheckAll.Checked = false;
			return;
		}
		if (text.ToUpper() == "ALL")
		{
			cbCheckAll.Checked = true;
			return;
		}
		string[] source = text.Split(',');
		int num = 0;
		foreach (TreeNode node in tvDevices.Nodes)
		{
			if (source.Contains(node.Name))
			{
				node.Checked = true;
				num++;
			}
		}
		if (num == tvDevices.Nodes.Count)
		{
			cbCheckAll.Checked = true;
		}
	}

	private void LaunchForm_Shown(object sender, EventArgs e)
	{
		tvDevices.SelectedNode = null;
	}

	private void InitDevices()
	{
		tvDevices.Nodes.Clear();
		List<Device> allDevices = Common.GetAllDevices();
		foreach (Device item in allDevices)
		{
			TreeNode node = new TreeNode
			{
				Name = item.Id.ToString(),
				Text = item.Name,
				ToolTipText = item.Desc,
				Tag = item
			};
			tvDevices.Nodes.Add(node);
		}
	}

	/// <summary>
	/// 全选/取消全选
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void cbCheckAll_CheckedChanged(object sender, EventArgs e)
	{
		foreach (TreeNode node in tvDevices.Nodes)
		{
			node.Checked = cbCheckAll.Checked;
		}
	}

	/// <summary>
	/// 确认
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnConfirm_Click(object sender, EventArgs e)
	{
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(Common.SETTING_FILE_FULL_PATH);
		XmlElement documentElement = xmlDocument.DocumentElement;
		XmlElement xmlElement = (XmlElement)documentElement.SelectSingleNode("Other");
		XmlElement xmlElement2 = (XmlElement)xmlElement.SelectSingleNode("DelayLogLimit");
		xmlElement2.InnerText = nudLimit.Value.ToString();
		xmlDocument.Save(Common.SETTING_FILE_FULL_PATH);
		List<Device> list = new List<Device>();
		foreach (TreeNode node in tvDevices.Nodes)
		{
			if (node.Checked)
			{
				list.Add((Device)node.Tag);
			}
		}
		if (!File.Exists(Common.MY_INI_PATH))
		{
			FileStream fileStream = File.Create(Common.MY_INI_PATH);
			fileStream.Close();
		}
		string empty = string.Empty;
		empty = ((list.Count == tvDevices.Nodes.Count) ? "ALL" : ((list.Count != 0) ? string.Join(",", list.Select((Device t) => t.Id)) : "NONE"));
		INIHelper.WriteValue("DelayLogDevices", "Ids", empty, Common.MY_INI_PATH);
		this.Save(list);
		Close();
	}

	/// <summary>
	/// 关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	/// <summary>
	/// ESC关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void LaunchForm_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Escape)
		{
			btnCancel_Click(null, null);
		}
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.tvDevices = new System.Windows.Forms.TreeView();
		this.btnConfirm = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.cbCheckAll = new System.Windows.Forms.CheckBox();
		this.nudLimit = new System.Windows.Forms.NumericUpDown();
		this.label2 = new System.Windows.Forms.Label();
		this.label1 = new System.Windows.Forms.Label();
		((System.ComponentModel.ISupportInitialize)this.nudLimit).BeginInit();
		base.SuspendLayout();
		this.tvDevices.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.tvDevices.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
		this.tvDevices.CheckBoxes = true;
		this.tvDevices.ItemHeight = 15;
		this.tvDevices.Location = new System.Drawing.Point(5, 34);
		this.tvDevices.Name = "tvDevices";
		this.tvDevices.ShowNodeToolTips = true;
		this.tvDevices.Size = new System.Drawing.Size(342, 386);
		this.tvDevices.TabIndex = 1;
		this.btnConfirm.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnConfirm.Location = new System.Drawing.Point(191, 426);
		this.btnConfirm.Name = "btnConfirm";
		this.btnConfirm.Size = new System.Drawing.Size(75, 23);
		this.btnConfirm.TabIndex = 3;
		this.btnConfirm.Text = "确认(&S)";
		this.btnConfirm.UseVisualStyleBackColor = true;
		this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.Location = new System.Drawing.Point(272, 426);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 4;
		this.btnCancel.Text = "取消(&C)";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.cbCheckAll.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
		this.cbCheckAll.AutoSize = true;
		this.cbCheckAll.Location = new System.Drawing.Point(5, 426);
		this.cbCheckAll.Name = "cbCheckAll";
		this.cbCheckAll.Size = new System.Drawing.Size(102, 16);
		this.cbCheckAll.TabIndex = 2;
		this.cbCheckAll.Text = "全选/取消全选";
		this.cbCheckAll.UseVisualStyleBackColor = true;
		this.cbCheckAll.CheckedChanged += new System.EventHandler(cbCheckAll_CheckedChanged);
		this.nudLimit.Location = new System.Drawing.Point(158, 7);
		this.nudLimit.Maximum = new decimal(new int[4] { 60000, 0, 0, 0 });
		this.nudLimit.Name = "nudLimit";
		this.nudLimit.Size = new System.Drawing.Size(40, 21);
		this.nudLimit.TabIndex = 0;
		this.nudLimit.Value = new decimal(new int[4] { 10, 0, 0, 0 });
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(204, 11);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(77, 12);
		this.label2.TabIndex = 2;
		this.label2.Text = "ms时记录日志";
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(3, 11);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(149, 12);
		this.label1.TabIndex = 0;
		this.label1.Text = "当网络通讯异常或延迟超过";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(351, 461);
		base.Controls.Add(this.nudLimit);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.cbCheckAll);
		base.Controls.Add(this.btnConfirm);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.tvDevices);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "DelayLogSettingForm";
		this.Text = "设置 - 记录设备延迟";
		base.Load += new System.EventHandler(LaunchForm_Load);
		base.Shown += new System.EventHandler(LaunchForm_Shown);
		base.KeyDown += new System.Windows.Forms.KeyEventHandler(LaunchForm_KeyDown);
		((System.ComponentModel.ISupportInitialize)this.nudLimit).EndInit();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
